/*
 * @(#)StandardAnalyzer.java	0.1 12/28/04
 *
 * Copyright 2004 Fabio Gasparetti. All rights reserved.
 */

package org.tabularium.text.se;

import org.apache.lucene.analysis.*;
import java.util.*;

import org.apache.lucene.analysis.standard.*;
import org.tabularium.text.analysis.*;

/**
 * Filters {@link StandardTokenizer} with {@link StandardFilter}, {@link
 * LowerCaseFilter} and {@link StopFilter}.
 * 
 * @version $Id: StandardAnalyzer.java,v 1.6 2003/11/10 14:31:19 otis Exp $
 */
public class StandardAnalyzer extends org.apache.lucene.analysis.Analyzer {
	protected Set stopSet = null;

	protected boolean porterStemmer = true;

	protected boolean numberFilter = true;

	protected boolean lowerCase = true;

	protected boolean stopList = true;
	
	protected boolean noAlphaFilter = true;

	protected int lengthFilter = 0;


	/** Builds an analyzer. */
	public StandardAnalyzer() {
		stopSet = new HashSet(Arrays.asList(StopList.defaultStoplist()));
	}

	/** Builds an analyzer with the given stop words. */
	public StandardAnalyzer(String[] stopWords) {
		stopSet = new HashSet(Arrays.asList(stopWords));
		//stopSet = StopFilter.makeStopSet(stopWords);
	}

	public void setStopList(String[] words) {
		//stopSet = StopFilter.makeStopSet(words);
	}
	
	public void enablePorterStemmer(boolean enabled) {
		porterStemmer = enabled;
	}

	public void enableNumberFilter(boolean enabled) {
		numberFilter = enabled;
	}

	public void enableLowerCase(boolean enabled) {
		lowerCase = enabled;
	}

	public void enableStopList(boolean enabled) {
		stopList = enabled;
	}

	public void enableNoAlphaFilter(boolean enabled) {
		noAlphaFilter = enabled;
	}

	/**
	 * If <code>len</code> is 0, the filter is disabled.
	 * 
	 * @param len
	 */
	public void lengthFilter(int len) {
		lengthFilter = len;
	}

	/**
	 * Constructs a {@link StandardTokenizer} filtered by a {@link
	 * StandardFilter}, a {@link LowerCaseFilter} and a {@link StopFilter}.
	 */
	public org.apache.lucene.analysis.TokenStream tokenStream(String fieldName, java.io.Reader reader) {
		org.apache.lucene.analysis.TokenStream result = new org.apache.lucene.analysis.standard.StandardTokenizer(
				reader);
		// Removes 's from the end of words
		// Removes dots from acronyms
		result = new StandardFilter(result);
		// converts to lower case
		if (lowerCase)
			result = new LowerCaseFilter(result);
		// filters out short tokens
		if (lengthFilter > 0)
			result = new LengthFilter(result, lengthFilter);
		// filters out numbers
		if (numberFilter)
			result = new NumberFilter(result);
		// filters out sequences with no alpha chs
		if (noAlphaFilter)
			result = new NoAlphaFilter(result);
		// stop list
		if (stopList)
			result = new StopFilter(result, stopSet);
		// porter stemmer
		if (porterStemmer)
			result = new PorterStemFilter(result);
		return result;

	}
}